/**
 * @date 2006-8-2
 */

package org.nanhill.moo.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nanhill.commons.model.pagination.Pagination;
import org.nanhill.moo.domain.User;
import org.nanhill.moo.service.UserService;
import org.nanhill.moo.web.form.PageUserForm;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/**
 * @author alin [xalinx at gmail dot com]
 * @date 2006-8-2
 */
public class PageUserController extends SimpleFormController {
	private static final String FORM_NAME = "pageUserForm";

	private UserService userService;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public PageUserController() {
		this.setCommandClass(PageUserForm.class);
		this.setCommandName(FORM_NAME);
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		PageUserForm form = (PageUserForm) command;
		Pagination<User> userPage = this.userService.loadPageLatest(form
				.getPageId(), form.getPageSize());
		Map<String, Object> viewModel = new HashMap<String, Object>();
		viewModel.put("userPage", userPage);
		return new ModelAndView(this.getSuccessView(), viewModel);
	}
}
